package test4_11.demo06;

import java.io.*;

public class Homework21_10 {

    //将Student对象转换成byte[],并将些byte数据写到文件中
    // 再写一个程序读出此byte数组,将此byte数组转换成Student.

    public static void TurnByteWriter() throws Exception {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);

        oos.writeObject(new Student("tom",18));

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D://a.txt"));

        byte[] allBytes = baos.toByteArray();
        bos.write(allBytes);
        bos.close();
        oos.close();

    }

    public static void ByteRead() throws Exception {

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D://a.txt"));

        byte[] allBytes = bis.readAllBytes();

        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(allBytes));

        Student student = (Student) ois.readObject();

        System.out.println(student);

        ois.close();
        bis.close();

    }

    public static void main(String[] args) throws Exception {

        Homework21_10.TurnByteWriter();
        Homework21_10.ByteRead();

    }
}
